package com.ss.android.account.b;

import android.app.Activity;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;

import com.bytedance.common.utility.UIUtils;
import com.bytedance.common.utility.collection.WeakHandler;
import com.ss.android.article.browser.R;

import java.io.File;

import inline.StorageUtils;

public final class b {
    public interface a {
        void p_();
    }

    public final Activity a;
    public final Resources b;
    final Fragment c;
    final File d;
    final WeakHandler e;
    private File f;
    private a g;

    public b(Activity arg4, Fragment arg5, WeakHandler arg6, a arg7) {

        this.a = arg4;
        this.c = arg5;
        this.e = arg6;
        this.g = arg7;
        File v0 = StorageUtils.e(((Context)arg4));
        File v1 = new File(v0, "head");
        if((v1.exists()) || (v1.mkdir())) {
            v0 = v1;
        }

        this.d = new File(v0, "avatar01.data");
        this.f = new File(v0, "avatar02.data");
        this.b = this.a.getResources();
    }

    private void a(Uri arg7, boolean arg8) {
        if(arg7 != null) {
            try {
            Intent v1 = new Intent("com.android.camera.action.CROP");
            if(arg8) {
            }else {
                String v0_1 = arg7.getLastPathSegment();
                if (Build.VERSION.SDK_INT >= 19 && !android.support.design.a.isTextEmpty(v0_1) && (v0_1.contains(":"))) {
                    v0_1 = v0_1.split(":")[1];
                }
                try {
                    arg7 = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, Long.parseLong(v0_1));
                } catch (Throwable v0_2) {
                }
            }
                v1.putExtra("return-data", false);
                v1.setDataAndType(arg7, "image/*");
                v1.putExtra("crop", "true");
                v1.putExtra("scale", true);
                v1.putExtra("aspectX", 1);
                v1.putExtra("aspectY", 1);
                v1.putExtra("outputX", 360);
                v1.putExtra("outputY", 360);
                v1.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
                v1.putExtra("noFaceDetection", true);
                v1.putExtra("scaleUpIfNeeded", true);
                this.f.delete();
                v1.putExtra("output", Uri.fromFile(this.f));
                if(this.c != null) {
                    this.c.startActivityForResult(v1, 10002);
                    return;
                }

                this.a.startActivityForResult(v1, 10002);
            }
            catch(Exception v0) {
            }
        }
    }

    public final boolean a(int arg11, int arg12, Intent arg13) {
        boolean v0;
        int v9 = R.string.photo_error_no_photo;
        int v8 = R.drawable.close_popup_textpage;
        if(arg11 == 10003) {
            if(arg12 == 0) {
                v0 = false;
                return v0;
            }

            if(arg13 == null) {
                return false;
            }

            Uri v0_1 = arg13.getData();
            String v3 = com.ss.android.account.bb.a().a(this.a, v0_1);
            if(android.support.design.a.isTextEmpty(v3)) {
                UIUtils.showImageToast(this.a, v8, v9);
                return false;
            }

            if(!new File(v3).exists()) {
                UIUtils.showImageToast(this.a, v8, v9);
                return false;
            }

            if("file".equals(v0_1.getScheme())) {
                v0_1 = com.ss.android.account.bb.a().a(this.a, v3);
            }

            this.a(v0_1, false);
            return true;
        }

        if(arg11 == 10004) {
            if(arg12 == 0) {
                return false;
            }

            try {
                this.a(Uri.fromFile(this.d), true);
            }
            catch(Exception v0_2) {
            }

          return false;
        }

        if(arg11 == 10002) {
            if(arg12 != -1) {
                return false;
            }

            if((this.f.exists()) && this.f.length() > 0) {
                String v0_3 = this.f.getAbsolutePath();
                if(this.g != null) {
                    this.g.p_();
                }

                new com.ss.android.account.b.d(this, v0_3).start();
                return true;
            }

            UIUtils.showImageToast(this.a, v8, v9);
            v0 = false;
        }
        else {
        label_49:
            v0 = false;
        }

        return v0;
    }
}

